//index.js
const app = getApp()

Page({
  onLoad: function(options) {
    console.log("index------------------------onLoad")
    const that = this
    // 判断是否是从视频通话页退出来
    if (options.reSub) {
      that.askSubMsg()
    } else {
      that.jump()
    }
  },

  jump: function() {
    const that = this
    that.waitLoadFinish = setInterval(() => {
      if (app.globalData.loadFinish) {
        clearInterval(that.waitLoadFinish)

        wx.hideLoading()

        if (app.globalData.registeredPhone == false || app.globalData.userInfo.nickName == null || app.globalData.userType == null) {
          wx.redirectTo({
            url: '/pages/register/register',
          })
        }
        if (app.globalData.userType == 0) {
          wx.redirectTo({
            url: "/pages/needHelp/index/index"
          })
        } else if (app.globalData.userType == 1) {
          wx.switchTab({
            url: "/pages/volunteer/index/index"
          })
        }
      }
    }, 100);
  },

  askSubMsg: function() {
    wx.showModal({
      title: '订阅提醒',
      content: '是否订阅下一次的视频求助提醒(建议您勾选 总是保持以上选择,不再询问 勾选后下次点击订阅不再屏幕底部的弹出确认框)',
      confirmText: '订阅',
      showCancel: true,
      success: (res) => {
        console.log(res)
        if (res.confirm) {
          wx.requestSubscribeMessage({
            tmplIds: ['V2N215C6c8dAg4qUxlZShk8VzxijNnRGUWop1BvB9Qo'],
            success(res) {
              console.log(res)
              if (res.errMsg == "requestSubscribeMessage:ok") {
                wx.showToast({
                  title: '订阅成功',
                  icon: 'success'
                })
              }
              setTimeout(() => {
                wx.switchTab({
                  url: "/pages/volunteer/index/index"
                })
              }, 200);
            },
            fail(err) {
              console.log(err)
              wx.switchTab({
                url: "/pages/volunteer/index/index"
              })
            }
          })
        } else {
          console.log('用户点击取消')
          wx.switchTab({
            url: "/pages/volunteer/index/index"
          })
        }
      },
    })
  }
})